package com.flute.haflute.monitor;

import java.util.HashMap;
import java.util.Map;
import java.util.TimerTask;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JvmReport {
	private static Logger logger = LoggerFactory.getLogger(JvmReport.class);
	String id;

	public JvmReport(String id) {
		this.id = id;
		send(id);
	}

	public static void send(final String id) {
		java.util.Timer timer = new java.util.Timer();
		TimerTask task = new TimerTask() {
			public void run() {
				try {
					Map<String, StatusMessage> map = new HashMap<String, StatusMessage>();

					java.text.NumberFormat numberFormat = java.text.NumberFormat
							.getPercentInstance();

					Memory memory = Memory.getInstance();
					// 内存总量
					double totalMemory = memory.getTotalMemory();
					// 已用内存
					double usedMemory = memory.getUsedMemory();
					String percentUsed = numberFormat
							.format(((double) usedMemory / (double) totalMemory));

					String content = "内存总量：" + String.valueOf(totalMemory)
							+ "MB " + "已用内存: " + String.valueOf(usedMemory)
							+ "MB " + "内存使用率: " + percentUsed;

					map.put(id, new StatusMessage("Jvm", content));
					MessageTransfer.localMsgSend("reportStatusMessage", map);
				} catch (Exception e) {
					logger.error("message send exception " + e);
				}
			}
		};
		timer.schedule(task, 10000, 10000);
	}
}
